<?php
class NddListController extends ezcMvcController
{
	public function doList()
	{
		$ret = new ezcMvcResult();

		//get db instance
		$psession = NddSession::getSession();

		$q = $psession->createFindQuery( 'NomDomaine' );
		$q->where( 'supprime = 0' )->orderBy( 'expiration' );
		
		$ndds = $psession->find( $q, 'NomDomaine' );
		
		$ret->variables['ndds'] = $ndds;
		$ret->variables['recherche'] = '';
		
		$_SESSION['lastSearch'] = $ndds;

		return $ret;
	}

	public function doSearch()
	{
		$ret = new ezcMvcResult();

		//get db instance
		$psession = NddSession::getSession();

		//create the search
		$q = $psession->createFindQuery( 'NomDomaine' );
		$e = $q->expr;
		
		if( isset($_POST['recherche']) ) {
			$recherche = trim($_POST['recherche']);
			$searchDate = true;
			
			try {
				$date = HelperDate::dateFrToMysql($recherche);
			}
			catch(CustomExceptionBadFormat $exception) {
				$searchDate = false;
			}
			
			//recherche d'une date ou d'un critère texte ?
			if( $searchDate ) {
				$q->where( 'supprime = 0', $e->lte( 'expiration', $q->bindValue($date) ) )
					->orderBy( 'expiration' );
			}
			else {
				$q->where( 'supprime = 0', $e->lOr( $e->like( 'domaine', $q->bindValue( '%'.$recherche.'%' ) ),
				$e->like( 'nom_client', $q->bindValue( '%'.$recherche.'%' ) ),
				$e->like( 'agresso', $q->bindValue( '%'.$recherche.'%' ) ) ) )
				->orderBy( 'expiration' );
			}
		}
		else {
			$recherche = '';

			$q->where( 'supprime = 0' )
			->orderBy( 'expiration' );
		}

		//search using the query
		$ndds = $psession->find( $q, 'NomDomaine' );
		
		$_SESSION['lastSearch'] = $ndds;

		$ret->variables['ndds'] = $ndds;
		$ret->variables['recherche'] = $recherche;

		return $ret;
	}
	
	public function doExport()
	{
		$ret = new ezcMvcResult();

		print_r($_SESSION);	exit;	

		return $ret;
	}
}

?>